package top.jingles.tools.deploytool.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import top.jingles.tools.deploytool.entity.Args;
import top.jingles.utils.args.DealFunction;
import top.jingles.utils.args.Option;
import top.jingles.utils.args.Parser;

public class ArgsParser {
    public Args Parse(String[] args) {
        final Args argsObj = new Args();

        Parser parser = new Parser();
        parser.setTitle("java -jar deploytool.jar");
        parser.addOption(new Option("v", "verbose", false, "打印详细信息", new DealFunction() {
            public boolean Deal(String argValue) {
                argsObj.setVerbose(true);
                return true;
            }
        }));
        parser.addOption(new Option("s", "setting", true, "加载的参数配置", new DealFunction() {
            public boolean Deal(String argValue) {
                String setting = argValue;
                argsObj.setSetting(setting);
                return true;
            }
        }));
        parser.addOption(new Option("d", "dir", true, "要扫描的目录", new DealFunction() {
            public boolean Deal(String argValue) {
                String path = argValue;
                argsObj.setPath(path);
                return true;
            }
        }));

        if (!parser.Parse(args)) {
            return argsObj;
        }

        // 验证参数文件的内容
        if (parseSetting(argsObj)) {
            argsObj.setValid(true);
        }

        checkArgs(argsObj);

        return argsObj;
    }

    protected boolean parseSetting(Args argsObj) {
        String settingFile = argsObj.getSetting();
        File file = new File(settingFile);
        if (!file.exists()) {
            return false;
        }
        Properties properties = new Properties();
        try {
            // InputStream inputStream = this.getClass().getResourceAsStream("/menu.properties");
            BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            properties.load(bf);
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return false;
        }

        String repositoryUrl = properties.getProperty("repository.url");
        String repositoryId = properties.getProperty("repository.id");
        String logFilePath = properties.getProperty("log.file.path");
        String errorLogFilePath = properties.getProperty("log.errorfile.path");
        String mvnPath = properties.getProperty("mvn.path");
        if (mvnPath == null || mvnPath.length() == 0) {
            mvnPath = "mvn";
        }
        argsObj.setRepositoryUrl(repositoryUrl);
        argsObj.setRepositoryId(repositoryId);
        argsObj.setLogFilePath(logFilePath);
        argsObj.setErrorLogFilePath(errorLogFilePath);
        argsObj.setMvnPath(mvnPath);

        return true;
    }

    public static void checkArgs(Args argsObj) {
        String setting = argsObj.getSetting();
        if (setting.length() == 0) {
            System.out.println("[ERROR]缺少必要的参数-setting指定参数配置文件!");
            System.exit(1);
        }
        // String path = argsObj.getPath();
        // if (path.length() == 0) {
        //     System.out.println("[ERROR]缺少必要的参数-path指定要扫描的目录!");
        //     System.exit(2);
        // }
        // File file = new File(path);
        // if (!file.exists()) {
        //     System.out.println("[ERROR]要扫描的目录 " + path + " 不存在!");
        //     System.exit(3);
        // }
        // if (!file.isDirectory()) {
        //     System.out.println("[ERROR]指定的目录 " + path + " 实际是文件，请指定一个目录!");
        //     System.exit(4);
        // }
        if (argsObj.getLogFilePath() == null || argsObj.getLogFilePath().length() == 0) {
            argsObj.setLogFilePath("/var/log/deploytool.log");
        }
        if (argsObj.getErrorLogFilePath() == null || argsObj.getErrorLogFilePath().length() == 0) {
            argsObj.setErrorLogFilePath("/var/log/deploytool-error.log");
        }
    }
}